package com.ssl.product.controller;

import com.ssl.common.pojo.CommodityEsBody;
import com.ssl.common.pojo.CommodityOnSaleBody;
import com.ssl.common.response.CommonResponse;
import com.ssl.common.response.ResponseCode;
import com.ssl.common.response.ResponseUtils;
import com.ssl.product.pojo.ProductSearchBody;
import com.ssl.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 产品
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 上架
     * @param onSaleBody
     * @return
     */
    @RequestMapping("/on-sale")
    public CommonResponse onSaleToEs(@RequestBody CommodityOnSaleBody onSaleBody) {
        List<CommodityEsBody> esBodies = onSaleBody.convertToEsBody();
        List<String> failedBulkList = productService.onSaleToEs(esBodies);
        if(failedBulkList.isEmpty()) {
            return ResponseUtils.okResponse(null);
        }
        return ResponseUtils.failResponse(ResponseCode.BAD_ESBULK_REQUEST.getCode(), failedBulkList, ResponseCode.BAD_ESBULK_REQUEST.getMessage());
    }

    /**
     * 查询es
     * @param productSearchBody
     * @return
     */
    @RequestMapping("/search")
    public CommonResponse search(@RequestBody ProductSearchBody productSearchBody) {
        return ResponseUtils.okResponse(productService.search(productSearchBody));
    }
}
